package com.easy.controller;

import com.easy.bean.Admin;
import com.easy.service.AdminService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin // 允许跨域访问
@RequestMapping("/admin")
// 只有 admin 角色才能访问该接口
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping
    public Result add(@RequestBody Admin admin) {
        Object obj = adminService.add(admin);
        return Result.success(obj);
    }

    @PutMapping
    public Result edit(@RequestBody Admin admin) {
        Object obj = adminService.edit(admin);
        return Result.success("编辑信息保存成功", obj);
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        Object obj = adminService.del(id);
        return Result.success("删除成功", obj);
    }

    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        Object obj = adminService.one(id);
        return Result.success(obj);
    }

    @GetMapping("/all")
    public Result all(Admin admin) {
        Object obj = adminService.all(admin);
        return Result.success(obj);
    }

    @GetMapping("/page")
    public Result page(Page page, Admin admin) {
        // 查询一共有多少条数据
        // 查询当前页要显示的数据
        adminService.page(page, admin);
        return Result.success(page);
    }
}
